import { type BrowserWindow } from "electron";

export const { registerWindowHolder, getWindow, getMainWindow } = (() => {
  const winds = new Map<number, BrowserWindow>();

  function registerWindowHolder(wind: BrowserWindow) {
    winds.set(wind.webContents.id, wind);
    wind.once("close", () => winds.delete(wind.webContents.id));
  }

  function getWindow(webContentsId: number) {
    return winds.get(webContentsId);
  }

  function getMainWindow() {
    return winds.get([...winds.keys()][0])!;
  }

  return {
    registerWindowHolder: registerWindowHolder,
    getWindow: getWindow,
    getMainWindow: getMainWindow,
  };
})();
